package org.jeecg.modules.common.service.impl;

import com.influxdb.client.InfluxDBClient;
import com.influxdb.client.QueryApi;
import com.influxdb.client.WriteApiBlocking;
import com.influxdb.client.domain.WritePrecision;
import com.influxdb.query.FluxTable;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.common.bean.InfluxDbEntity;
import org.jeecg.modules.common.service.InFluxDBService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Classname InFluxDbServiceImpl
 * @Description TODO
 * @Date 2022/12/4 12:33
 * @Created by 10835
 */
@Slf4j
@Service
public class InFluxDbServiceImpl implements InFluxDBService {
    @Resource
    private InfluxDBClient influxDBClient;
    @Override
    public <E extends InfluxDbEntity> void insert(E e) {
        WriteApiBlocking writeApi = influxDBClient.getWriteApiBlocking();
        writeApi.writeMeasurement( WritePrecision.NS, e);
        log.info("InFluxDB插入数据{}",e.toString());


    }

    @Override
    public List<FluxTable> select(String flux) {
        QueryApi queryApi = influxDBClient.getQueryApi();
        List<FluxTable> tables = queryApi.query(flux);
        log.info("InFluxDB查询数据{}",tables);
        return tables;
    }
}
